let DateUtils = {
  // 日期格式化方法
  formatDate: function (date, format) {
    let paddNum = function (num) {
      num += ''
      return num.replace(/^(\d)$/, '0$1')
    }
    // 指定格式字符
    let cfg = {
      yyyy: date.getFullYear() // 年 : 4位
      ,
      yy: date.getFullYear().toString().substring(2)// 年 : 2位
      ,
      M: date.getMonth() + 1 // 月 : 如果1位的时候不补0
      ,
      MM: paddNum(date.getMonth() + 1) // 月 : 如果1位的时候补0
      ,
      d: date.getDate() // 日 : 如果1位的时候不补0
      ,
      dd: paddNum(date.getDate())// 日 : 如果1位的时候补0
      ,
      hh: paddNum(date.getHours()) // 时
      ,
      mm: paddNum(date.getMinutes()) // 分
      ,
      ss: date.getSeconds()
      // 秒
    }
    format || (format = 'yyyy-MM-dd hh:mm:ss')
    return format.replace(/([a-z])(\1)*/ig, function (m) {
      return cfg[m]
    })
  },
  int2Str: function (time, format) {
    let date = new Date(time)
    return DateUtils.formatDate(date, format)
  },
  formatDate1: function (str, format) {
    let obj = str.split(/[: -]/)
    let dInt = new Date(obj[0], parseInt(obj[1]) - 1, obj[2], '00', '00', '00')
    let nowInt = new Date().getTime()
    let temp = parseInt((nowInt - dInt.getTime()) / 1000 / 60 / 60 / 24)
    if (temp < 1) {
      return '今天'
    } else if (temp < 2) {
      return '昨天'
    } else if (temp < 7) {
      return '一周内'
    } else {
      if (Utils.isEmpty(format)) {
        format = 'yyyy-MM-dd hh:mm:ss'
      }
      return DateUtils.formatDate(new Date(dInt.getTime()), format)
    }
  },

  int2TimeDesc(dInt, flag) {//时间戳，转为时间描述
    if (!dInt) {
      return ''
    }
    let dt = new Date()
    let isDateInt = parseInt(new Date(new Date(dInt * 1000).toLocaleDateString()).getTime() / 1000)//获取当天0点的时间戳

    let nowInt = parseInt(dt.getTime() / 1000)//获取当前时间戳
    let nowDateInt = parseInt(new Date(dt.toLocaleDateString()).getTime() / 1000)//获取当天0点的时间戳

    let temp = nowInt - dInt
    let dtTemp = nowInt - isDateInt
    if (temp < 60) {
      return '刚刚'
    } else if (temp < 60 * 60) {
      return (Math.floor(temp / 60)) + '分钟前'
    } else if (temp < 60 * 60 * 24) {
      return (Math.floor(temp / (60 * 60))) + '小时前'
    } else if (temp < 60 * 60 * 24 * 2 &&  (nowInt - nowDateInt) > (dInt - isDateInt)) {
      //发布时间大于当前时间
      return '昨天'
    } else if (dtTemp < 60 * 60 * 24 * 3) {
      return '前天'
    } else {
      return DateUtils.formatDate(new Date(dInt * 1000), 'yyyy-MM-dd hh:mm')
    }
  },
  int2TimeTopic(dInt, flag) {//时间戳，转为时间描述
    if (!dInt) {
      return ''
    }
    let dt = new Date()
    let isDateInt = parseInt(new Date(new Date(dInt * 1000).toLocaleDateString()).getTime() / 1000)//获取当天0点的时间戳

    let nowInt = parseInt(dt.getTime() / 1000)//获取当前时间戳
    let nowDateInt = parseInt(new Date(dt.toLocaleDateString()).getTime() / 1000)//获取当天0点的时间戳

    let temp = nowInt - dInt
    let dtTemp = nowInt - isDateInt
    let rDt = ''
    if (temp < 60) {
      rDt = '刚刚'
    } else if (temp < 60 * 60) {
      rDt = (Math.floor(temp / 60)) + '分钟前'
    } else if (temp < 60 * 60 * 24) {
      rDt = (Math.floor(temp / (60 * 60))) + '小时前'
    } else if (temp < 60 * 60 * 24 * 2 &&  (nowInt - nowDateInt) > (dInt - isDateInt)) {
      //发布时间大于当前时间
      rDt = '昨天'
    } else if (dtTemp < 60 * 60 * 24 * 3) {
      rDt = '前天'
    } else if (dtTemp < 60 * 60 * 24 * 7) {
      rDt = '3天前'
    } else if (dtTemp < 60 * 60 * 24 * 30) {
      rDt = '1周前'
    } else if (dtTemp < 60 * 60 * 24 * 90) {
      rDt = '1月前'
    } else {
      rDt = '3月前'
    }
    return rDt + '更新'
  },
  //月份增加
  monthAdd: function (str, i, format) {
    let obj = str.split(/[: -]/)
    let myDate = new Date(parseInt(obj[0]), parseInt(obj[1]) - 1, obj[2], obj[3], obj[4], obj[5])

    myDate.setMonth(parseInt(obj[1]) - 1 + i)

    return DateUtils.formatDate(myDate, format)
  },
  int2TimeStr: function (second) {
    second = parseInt(second, 10)
    let time = ''
    let hour = second / 3600 | 0
    if (hour !== 0) {
      time += DateUtils.checkTime(hour) + ':'
    }
    let min = (second % 3600) / 60 | 0
    time += DateUtils.checkTime(min) + ':'
    let sec = (second - hour * 3600 - min * 60) | 0
    time += DateUtils.checkTime(sec)
    return time
  },
  str2Date :function(str) {
    let obj = str.split(/[: -]/)
    let dInt =  new Date(obj[0], parseInt(obj[1]) - 1, obj[2], Utils.isEmpty(obj[3]) ? 0 : obj[3], Utils.isEmpty(obj[4]) ? 0 : obj[4], Utils.isEmpty(obj[5]) ? 0 : obj[5])

    let dataValue = new Date(dInt.getTime())
    return dataValue
  },
  getDttm : function () {
    return new Date().getTime() / 1000
  },
  checkTime: function (num) {
    let n = Number(num)
    if (n < 10)
    {n = '0' + n}
    return n
  },
  //获取date的前n天
  getLastDate : function(date, n, flag) {
    let dataValue

    if (flag) {
      dataValue = new Date(date.getTime() + (1000 * 60 * 60 * 24) * n)
    } else {
      dataValue = new Date(date.getTime() - (1000 * 60 * 60 * 24) * n)
    }

    return dataValue
  },
  //获取日期对应的星期
  getWeekDay : function(date) {
    let days = date.getDay()
    switch(days){
    case 1:
      days = '星期一'
      break
    case 2:
      days = '星期二'
      break
    case 3:
      days = '星期三'
      break
    case 4:
      days = '星期四'
      break
    case 5:
      days = '星期五'
      break
    case 6:
      days = '星期六'
      break
    case 0:
      days = '星期日'
      break
    }
    return days
  },
  getNowInt : function () {//获取当前时间戳
    let time = Date.parse(new Date())
    return time / 1000
  },
  date2Dt : function (date) {//匹配数据库，需要加8个小时
    let myDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), '00', '00', '00')
    return myDate.getTime() / 1000 + 8 * 60 * 60
  },
  s2Str : function (val) {
    if (Utils.isEmpty(val)) {
      return ''
    } else if (val.indexOf('：') > 0) {
      return Utils.replaceAll(val,'：', ':')
    } else if (/(^[1-9]\d*$)/.test(val)) {//判断如果是数字，秒
      let theTime = parseInt(val / 1000)// 秒
      let middle = 0// 分
      let hour = 0// 小时

      if(theTime > 60) {
        middle = parseInt(theTime / 60)
        theTime = parseInt(theTime % 60)
        if(middle > 60) {
          hour = parseInt(middle / 60)
          middle = parseInt(middle % 60)
        }
      }
      let result = parseInt(theTime) < 10 ? '0' + theTime : '' + theTime
      if(middle > 0) {
        result = (parseInt(middle) < 10 ? '0' + middle : '' + middle) + ':' + result
      }
      if(hour > 0) {
        result = (parseInt(hour) < 10 ? '0' + hour : '' + hour) + ':' + result
      }
      return result
    } else {
      return val
    }
  },

  str2Dt : function (str) {
    let obj = str.split(/[: -]/)
    let myDate = new Date(parseInt(obj[0]), parseInt(obj[1]) - 1, obj[2], obj[3], obj[4], obj[5])
    return myDate.getTime() / 1000
  },

  //获得当前时间前n天的日期  出参举例：20200909
  getLastDateForm : function (n) {
    //当前时间的前n天
    let tmpDate = DateUtils.getLastDate(DateUtils.str2Date(DateUtils.int2Str(DateUtils.getNowInt() * 1000)), n, true)
    let resDate = DateUtils.formatDate( tmpDate, 'yyyyMMdd')
    return resDate
  }
}
